﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WPF_TEST
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 單例，返回實例
        /// </summary>
        internal static readonly MainWindow SingleCase=new MainWindow();
 
        public MainWindow()
        {
            InitializeComponent();
            //不显示在系统任务栏
            this.ShowInTaskbar = false;
 
            //設置啟動時在屏幕中心
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //顯示垂直滾動條
            richTxt01.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            //註冊關閉事件，退出程序，防止沒有退出釋放情況
            //this.Closed += MainWindow_Closed;
            this.Closing += MainWindow_Closing;

            //窗口最小化事件
            this.StateChanged += new EventHandler((object o,EventArgs e)=> {
                if (WindowState == WindowState.Minimized)
                {
                    Hide();                    
                }
            });

            richTxt01.AppendText("武松" + System.Environment.NewLine);
            richTxt01.AppendText("正在執行數據查詢..." + System.Environment.NewLine);
            richTxt01.AppendText("查詢到結果239條..." + System.Environment.NewLine);
            Task.Run(() =>
            {
                while (true)
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        richTxt01.AppendText("武松" + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString() + System.Environment.NewLine);
                        richTxt01.ScrollToEnd();
                    }));
                    Thread.Sleep(2000);
                }
            });
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //取消关闭窗体
            e.Cancel = true;
            //将窗体变为最小化
            //this.WindowState = WindowState.Minimized;
            ////不显示在系统任务栏
            //this.ShowInTaskbar = false;
            Hide();
        }

        //private void MainWindow_Closed(object sender, EventArgs e)
        //{
        //    System.Windows.Application.Current.Shutdown();
        //    Environment.Exit(0);
        //}

    }
}
